package servlet;

import bean.NewsofUser;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class UserListServlet
 */
@WebServlet("/testlimit")
public class NewsLimitServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewsLimitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer page = 1;
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");
        try {
            page = Integer.parseInt(request.getParameter("p"));
        } catch (Exception e) {
        }
        Integer pageSize = 7;
        try {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        } catch (Exception e) {
        }
        Integer start = (page - 1) * pageSize;
        try {
            System.out.println(start + " " + pageSize);
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/press_release_system?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true",
                    "root", "123456");
            PreparedStatement ps = conn.prepareStatement("select * from news_info_view limit ?,?");
            ps.setObject(1, start);
            ps.setObject(2, pageSize);
            ResultSet rs = ps.executeQuery();
            List<NewsofUser> list = new ArrayList<NewsofUser>();
            while (rs.next()) {
                NewsofUser newsofUser = new NewsofUser();
                newsofUser.setNewsid(rs.getInt("newsid"));
                newsofUser.setNewstitle(rs.getString("newstitle"));
                newsofUser.setNewscontent(rs.getString("newscontent"));
                newsofUser.setUsername(rs.getString("username"));
                newsofUser.setPubtime(rs.getDate("pubtime"));
                newsofUser.setNewsimg(rs.getString("newsimg"));
                newsofUser.setUserid(rs.getInt("userid"));

                list.add(newsofUser);
            }

            ps = conn.prepareStatement("select count(*) from news_info_view");
            rs = ps.executeQuery();
            Integer total = 0;
            if (rs.next()) {
                total = rs.getInt(1);
            }

//            Integer tp = total / pageSize;
            Integer tp = total/ pageSize;
            if (total % pageSize != 0) tp++;

            StringBuilder sb = new StringBuilder();

            if (page > 1) {
                sb.append("<a href=\"../testlimit?p=1\">首页</a>&nbsp;&nbsp;");
                sb.append("<a href=\"../testlimit?p=" + (page - 1) + "\">上一页</a>&nbsp;&nbsp;");
            }

            for (int i = 1; i <= tp; i++) {
                if (i == page) {
                    sb.append("<b>" + i + "</b>&nbsp;&nbsp;");
                } else {
                    sb.append("<a href=\"../testlimit?p=" + i + "\">" + i + "</a>&nbsp;&nbsp;");
                }
            }

            if (page < tp) {
                sb.append("<a href=\"../testlimit?p=" + (page + 1) + "\">下一页</a>&nbsp;&nbsp;");
                sb.append("<a href=\"../testlimit?p=" + tp + "\">尾页</a>&nbsp;&nbsp;");
            }

            request.getSession().setAttribute("pageStr", sb.toString());
            request.getSession().setAttribute("newsselectlist", list);
            request.getSession().setAttribute("start", start);

            response.sendRedirect("./jsp/News.jsp"); // Redirect to news page
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
